package com.szholly.pro.watergas_server.ps.android.service;

import java.util.ArrayList;
import java.util.List;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.ps.android.bean.QcTaskParam;
import com.szholly.pro.watergas_server.ps.android.common.NonNullUtil;
import com.szholly.pro.watergas_server.ps.android.common.ValidUtils;
import com.szholly.pro.watergas_server.ps.android.controller.QcControl;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

public class QcBlindOperationService implements QcOperationService {

	@Override
	public List<RowBase> getOperationTaskList(QcTaskParam taskParam) throws Exception {
		String userId = taskParam.getUserId();
		String taskTypeCode = taskParam.getTaskTypeCode();
		String companyCode = taskParam.getCompanyCode();
		ValidUtils.hasText(userId, "用户编号为空，获取任务列表失败");
		ValidUtils.hasText(taskTypeCode, "任务类型编号为空，获取任务列表失败");
		ValidUtils.hasText(companyCode, "公司编号为空，获取任务列表失败");

		String jobId = null;
		if (QcControl.BLIND.equals(taskTypeCode)) {
			jobId = QcControl.BLIND.getJobId();
		} else if (QcControl.SPOT.equals(taskTypeCode)) {
			jobId = QcControl.SPOT.getJobId();
		}
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		parms.add(new GlobalParameter("jobId", jobId));
		parms.add(new GlobalParameter("companyCode", companyCode));
		String sql = "select t.TASK_CODE \"id\",\n"
				+ "       case\n"
				+ "         when t1.value is null then\n"
				+ "          '待处理'\n"
				+ "         else\n"
				+ "          '已处理'\n"
				+ "       end \"name\",\n"
				+ "       case\n"
				+ "         when t1.value is null then\n"
				+ "          '0'\n"
				+ "         else\n"
				+ "          '1'\n"
				+ "       end \"type\",\n"
				+ "       t.TASK_DESC \"des\",\n"
				+ "       to_char(t.START_TIME, 'yyyy-mm-dd hh24:mi:ss') \"startTime\",\n"
				+ "       to_char(t.END_TIME, 'yyyy-mm-dd hh24:mi:ss') \"endTime\",\n"
				+ "       t.FINISH_BY \"userId\"\n"
				+ "  from OP_TASK t\n"
				+ "  left join QC_WATER_THAN t1\n"
				+ "    on t1.operation_task_code = t.task_code\n"
				+ " where 1 = 1\n"
				+ "   and t.task_status = '1'\n"
				+ "   and t1.companyid = @companyCode\n"
				+ "   and t.JOB_ID = @jobId\n"
				+ "order by case\n"
				+ "            when t.end_time is null then\n"
				+ "             t.start_time\n"
				+ "            else\n"
				+ "             t.end_time\n"
				+ "          end desc";
		List<RowBase> rows = DataUtil.getSelectRows(sql, parms);
		for (RowBase rowBase : rows) {
			String des = (String) rowBase.get("des");
			try {
				String[] split = des.split("  ");
				rowBase.put("des", split[1]);
			} catch (Exception e) {
			}
		}
		return NonNullUtil.toList(rows);
	}

	@Override
	public RowBase addOperationTask(QcTaskParam taskParam) throws Exception {
		throw new Exception("该任务会主动推送");
	}

	@Override
	public void deleteOperationTask(QcTaskParam taskParam) throws Exception {
		throw new Exception("该任务无法删除");
	}
}
